package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCustomProcessBaseMachineRel
//@description: 创建CustomProcessBaseMachineRel记录
//@param: customProcessBaseMachineRel model.CustomProcessBaseMachineRel
//@return: err error

func CreateCustomProcessBaseMachineRel(customProcessBaseMachineRel model.CustomProcessBaseMachineRel) (err error) {
	err = global.GVA_DB.Create(&customProcessBaseMachineRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseMachineRel
//@description: 删除CustomProcessBaseMachineRel记录
//@param: customProcessBaseMachineRel model.CustomProcessBaseMachineRel
//@return: err error

func DeleteCustomProcessBaseMachineRel(customProcessBaseMachineRel model.CustomProcessBaseMachineRel) (err error) {
	err = global.GVA_DB.Delete(customProcessBaseMachineRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseMachineRelByIds
//@description: 批量删除CustomProcessBaseMachineRel记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCustomProcessBaseMachineRelByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CustomProcessBaseMachineRel{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCustomProcessBaseMachineRel
//@description: 更新CustomProcessBaseMachineRel记录
//@param: customProcessBaseMachineRel *model.CustomProcessBaseMachineRel
//@return: err error

func UpdateCustomProcessBaseMachineRel(customProcessBaseMachineRel model.CustomProcessBaseMachineRel) (err error) {
	err = global.GVA_DB.Save(&customProcessBaseMachineRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseMachineRel
//@description: 根据id获取CustomProcessBaseMachineRel记录
//@param: id uint
//@return: err error, customProcessBaseMachineRel model.CustomProcessBaseMachineRel

func GetCustomProcessBaseMachineRel(id uint) (err error, customProcessBaseMachineRel model.CustomProcessBaseMachineRel) {
	err = global.GVA_DB.Where("id = ?", id).First(&customProcessBaseMachineRel).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseMachineRelInfoList
//@description: 分页获取CustomProcessBaseMachineRel记录
//@param: info request.CustomProcessBaseMachineRelSearch
//@return: err error, list interface{}, total int64

func GetCustomProcessBaseMachineRelInfoList(info request.CustomProcessBaseMachineRelSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CustomProcessBaseMachineRel{})
    var customProcessBaseMachineRels []model.CustomProcessBaseMachineRel
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.BaseId != 0 {
        db = db.Where("`base_id` = ?",info.BaseId)
    }
    if info.MachineId != 0 {
        db = db.Where("`machine_id` = ?",info.MachineId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&customProcessBaseMachineRels).Error
	return err, customProcessBaseMachineRels, total
}